'''
Created on 26 mai 2011

@author: rdionisi
'''

from django.contrib.auth.models import User
from django.db import models
from wikiproduct import Ratings
import datetime
import sys

class Company(models.Model):
    unique_name = models.CharField(max_length=200,primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.CharField(max_length=2000,blank=True)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.name

   
class UploadModel(models.Model):
    title = models.CharField(max_length=64, blank=True)
    file = models.FileField(upload_to='uploads/%Y/%m/%d/%H/%M/%S/')

    @property
    def filename(self):
        return self.file.name.rsplit('/', 1)[-1]
    def __unicode__(self):
        return self.title
    
class Product(models.Model):
    unique_name = models.CharField(max_length=200,primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.TextField(max_length=2000,blank=True)
    customer_services = models.TextField(max_length=2000)
    company = models.ForeignKey(Company,null=True)
    full_weight = models.FloatField(null=True)
    nude_weight = models.FloatField(null=True)
    image = models.ForeignKey(UploadModel,null=True)
    completed = models.IntegerField(default=0)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.name
    def ingredient_ratings(self):
        return Ratings.ingredient_ratings(self)
    def ingredient_ratings_detail(self):
        return Ratings.ingredient_ratings_detail(self)
    def packaging_ratings(self):
        return Ratings.packaging_ratings(self)
    def weight_ratings(self):
        return Ratings.weight_ratings(self)
    def weight_percent(self):
        return Ratings.weight_percent(self)
    def ratings(self):
        return Ratings.ratings(self)
    def ratings_max_60(self):
        result = Ratings.ratings(self)
        if (result>60):
            return 60
        return result

class Ingredient(models.Model):
    unique_name = models.CharField(max_length=200,primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.TextField(max_length=2000,blank=True)
    rating = models.IntegerField(default=0)
    link = models.BooleanField(default=True)

    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.name

class IngredientAttribute(models.Model):
    TYPE_ING = (
        (u'vg', u'Vegetarien'),
        (u'nvg', u'Non Vegetarien'),
    )
    type = models.CharField(max_length=5, choices=TYPE_ING)
    value = models.CharField(max_length=200,blank=True)
    detail = models.TextField(max_length=2000,blank=True)
    ingredient = models.ForeignKey(Ingredient)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.ingredient.name +'/'+ self.type

class ProductIngredient(models.Model):
    ingredient = models.ForeignKey(Ingredient)
    product = models.ForeignKey(Product)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    
    def __unicode__(self):
        return self.ingredient.name +'/'+ self.product.name

class ProductIngredientAttribute(models.Model):
    TYPE_ING = (
        (u'dist', u'Distance'),
    )
    type = models.CharField(max_length=5, choices=TYPE_ING)
    value = models.CharField(max_length=200,blank=True)
    detail = models.TextField(max_length=2000,blank=True)
    product_ingredient = models.ForeignKey(ProductIngredient)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.ingredient.name +'/'+ self.product.name +'/'+ self.type

class Packaging(models.Model):
    unique_name = models.CharField(max_length=200,primary_key=True)
    name = models.CharField(max_length=200)
    detail = models.TextField(max_length=2000,blank=True)
    rating = models.IntegerField(default=1)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
    def __unicode__(self):
        return self.name


class ProductPackaging(models.Model):
    packaging = models.ForeignKey(Packaging)
    product = models.ForeignKey(Product)
    weight = models.FloatField(blank=True)
    
    pub_date = models.DateTimeField('date published',default=datetime.datetime.today())
    version = models.IntegerField(default=1)
    user = models.ForeignKey(User,default=User)
    deleted = models.BooleanField()
 